package com.excuse.excusebuilder.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.base.BaseActivity;
import com.excuse.excusebuilder.commen.util.MyToast;
import com.excuse.excusebuilder.impl.view.MainActivityInterface;
import com.excuse.excusebuilder.presenter.MainActivityPresenter;

import butterknife.BindView;
import butterknife.OnClick;


public class MainActivity extends BaseActivity implements MainActivityInterface {

    private static boolean isExit = false;  // 定义一个变量，来标识是否退出
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.frame)
    FrameLayout frame;
    @BindView(R.id.home_img)
    ImageView homeImg;
    @BindView(R.id.home_text)
    TextView homeText;
    @BindView(R.id.home_layout)
    LinearLayout homeLayout;
    @BindView(R.id.community_img)
    ImageView communityImg;
    @BindView(R.id.community_text)
    TextView communityText;
    @BindView(R.id.community_layout)
    LinearLayout communityLayout;
    @BindView(R.id.mine_img)
    ImageView mineImg;
    @BindView(R.id.mine_text)
    TextView mineText;
    @BindView(R.id.mine_layout)
    LinearLayout mineLayout;
    @BindView(R.id.bottom_bar)
    LinearLayout bottomBar;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private MainActivityPresenter presenter;

    @Override
    protected void initData() {
        initPresenter();
        clickHome();
        presenter.setHomeFragment();
        //presenter.signIn();    //方便开发，后期删除
    }

    private void initPresenter() {
        presenter = new MainActivityPresenter(context, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_main;
    }

    @OnClick({R.id.home_layout, R.id.community_layout, R.id.mine_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_layout:
                reductionBottom();
                clickHome();
                presenter.setHomeFragment();

                break;
            case R.id.community_layout:
                reductionBottom();
                clickCommunity();
                presenter.setCommunityFragment();
                break;
            case R.id.mine_layout:
                reductionBottom();
                clickMine();
                presenter.setMineFragment();
                break;
        }
    }

    private void clickMine() {
        mineImg.setImageResource(R.mipmap.people_click);
        toolbar.setTitle(R.string.mine_text);
        toolbar.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
    }

    private void clickCommunity() {
        communityImg.setImageResource(R.mipmap.community_click);
        communityText.setTextColor(getResources().getColor(R.color.md_light_blue_500));
        toolbar.setTitle(R.string.community_text);
        toolbar.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
    }

    private void clickHome() {
        homeImg.setImageResource(R.mipmap.home_click);
        homeText.setTextColor(getResources().getColor(R.color.md_light_blue_500));
        toolbar.setTitle(R.string.home_text);
        toolbar.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
    }

    private void reductionBottom() {
        homeImg.setImageResource(R.mipmap.home);
        communityImg.setImageResource(R.mipmap.community);
        mineImg.setImageResource(R.mipmap.people);
        homeText.setTextColor(getResources().getColor(R.color.md_grey_600));
        communityText.setTextColor(getResources().getColor(R.color.md_grey_600));
        mineText.setTextColor(getResources().getColor(R.color.md_grey_600));
    }

    //捕获back，设置按第二次退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            MyToast.showToast(this, "再按一次退出程序");
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public FragmentManager getActivityFragmentManager() {
        return getSupportFragmentManager();
    }
}
